package com.inspur.edp.formserver.vmmanager.linkedconfig;

import com.inspur.edp.cef.designtime.api.exceptions.CefException;;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.GspViewObject;
import com.inspur.edp.formserver.viewmodel.collection.ValueHelpConfigCollection;
import com.inspur.edp.formserver.viewmodel.common.ValueHelpConfig;
import com.inspur.edp.formserver.viewmodel.linkedconfig.LinkedConfig;
import com.inspur.edp.formserver.viewmodel.linkedconfig.LinkedConfigCollection;
import com.inspur.edp.formserver.viewmodel.linkedconfig.service.LinkedConfigService;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import io.iec.edp.caf.commons.exception.ExceptionLevel;


import static com.inspur.edp.formserver.viewmodel.common.LinkedHelpConfigUtil.getObjectByCode;
import static com.inspur.edp.formserver.viewmodel.common.LinkedHelpConfigUtil.getSourceFieldId;

public class LinkedConfigImp implements LinkedConfigService {
    @Override
    public void handleLinkedConfig( GspMetadata sourceMetadata) {
        GspViewModel model  = (GspViewModel) sourceMetadata.getContent();
        ValueHelpConfigCollection valueHelpConfigs = model.getValueHelpConfigs();
        if (valueHelpConfigs==null || valueHelpConfigs.size()==0)
            return;
        for (ValueHelpConfig valueHelpConfig : valueHelpConfigs) {
            handleMappingField(valueHelpConfig,sourceMetadata);
        }
    }

    private void handleMappingField(ValueHelpConfig helpConfig, GspMetadata sourceMetadata) {
        if (helpConfig.getLinkedConfigs()==null || helpConfig.getLinkedConfigs().size()==0){
            return;
        }
        GspViewModel sourceviewModel= (GspViewModel) sourceMetadata.getContent();
        LinkedConfigCollection linkedConfigs = helpConfig.getLinkedConfigs();
        for (LinkedConfig linkedConfig : linkedConfigs) {
            handleLinkedConfigField(linkedConfig,sourceviewModel);
        }
    }

    private void handleLinkedConfigField(LinkedConfig linkedConfig, GspViewModel sourceviewModel ) {
        String sourceField = linkedConfig.getLinkedFieldsMapping().getSourceField();
        if(sourceField==null || "".equals(sourceField))
            throw new CefException("","帮助级联字段不能为空，请重新选择",null,ExceptionLevel.Error,true);
        String sourceObjCode = linkedConfig.getLinkedFieldsMapping().getSourceObjCode();
        if(sourceObjCode==null || "".equals(sourceObjCode))
            throw new CefException("","帮助级联实体编号不能为空，请重新选择",null,ExceptionLevel.Error,true);
        GspViewObject sourceObject=getObjectByCode(sourceviewModel,sourceObjCode);
        if (sourceObject==null)
            throw new CefException("",String.format("未获取到编号为%1$s的实体对象",sourceObjCode),null,ExceptionLevel.Error,true);
        String sourceFieldId = getSourceFieldId(sourceObject, sourceField);
        if (sourceFieldId==null ||"".equals(sourceFieldId)){
            throw new CefException("",String.format("未获取到标签为%1$s的字段的唯一标识",sourceFieldId),null,ExceptionLevel.Error,true);
        }
        linkedConfig.getLinkedFieldsMapping().setSourceField(sourceFieldId);
    }

}
